<template>
  <div class="app-container">
        <el-dialog title="赠送优惠券" :visible.sync="open" width="50%" append-to-body :close-on-click-modal='false'>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="优惠券名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入优惠券名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="优惠券名称" align="center" prop="name" />
      <el-table-column label="面值" align="center" prop="couponMoney" />
      <el-table-column label="剩余张数" align="center" prop="couponNumber" />
      <el-table-column label="使用门槛" align="center" prop="sill" >
        <template slot-scope="scope">
          <span v-if="scope.row.expirationType===1">无限制</span>
          <span v-else-if="scope.row.expirationType===2">
            满{{ scope.row.sillMoney }}元可用
          </span>
        </template>
      </el-table-column>
      <el-table-column label="有效值" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.expirationType===1">无限制</span>
          <span v-else-if="scope.row.expirationType===2">
            {{ (scope.row.expirationTimes&&scope.row.expirationTimes.length>0)?
            parseTime(scope.row.expirationTimes[0]) +' 至 '+ parseTime(scope.row.expirationTimes[1])+'时间内使用':'--' }}
          </span>
          <span v-else-if="scope.row.expirationType===3">
             {{ scope.row.expirationVal+'天内使用' }}
          </span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
        </el-dialog>
  </div>
</template>

<script>
import { listCoupon, getCoupon, delCoupon, addCoupon, updateCoupon, exportCoupon } from "@/api/system/coupon";

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 优惠券管理表格数据
      couponList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    init(){
      this.open = true
    },
    /** 查询优惠券管理列表 */
    getList() {
      this.loading = true;
      listCoupon(this.queryParams).then(response => {
        this.couponList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    }
  }
};
</script>